﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hallenberg.SmartClient.Interfaces;
using Microsoft.Practices.Unity;
using System.Diagnostics;

namespace Hallenberg.SmartClient.Services
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")]
    class ServiceLocator : IServiceLocator, IDisposable
    {
        private IUnityContainer m_UnityContainer;

        public ServiceLocator()
        {
            m_UnityContainer = new UnityContainer();
        }

        public void RegisterType<T>()
        {
            m_UnityContainer.RegisterType<T>();
        }

        public void RegisterType<T>(string mappingName)
        {
            m_UnityContainer.RegisterType<T>(mappingName);
        }

        public void RegisterType<TI, TA>() where TA : TI
        {
            m_UnityContainer.RegisterType<TI, TA>();
        }

        public void RegisterType<TI, TA>(string mappingName) where TA : TI
        {
            m_UnityContainer.RegisterType<TI, TA>(mappingName);
        }

        public void RegisterType(Type lookupType, Type actualType, string mappingName)
        {
            m_UnityContainer.RegisterType(lookupType, actualType, mappingName);
        }

        public void RegisterInstance<T>(T instance)
        {
            m_UnityContainer.RegisterInstance<T>(instance);
        }

        public void RegisterInstance<T>(string mappingName, T instance)
        {
            m_UnityContainer.RegisterInstance<T>(mappingName, instance);
        }

        public T Resolve<T>()
        {
            return m_UnityContainer.Resolve<T>();
        }

        public object Resolve(Type type)
        {
            return m_UnityContainer.Resolve(type);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public IEnumerable<T> ResolveAll<T>()
        {
            return m_UnityContainer.ResolveAll<T>();
        }

        public void ChangeDependencyInjector(IDependencyInjector dependencyInjector)
        {
            Debug.Assert(dependencyInjector != null);
            if (dependencyInjector == null)
            {
                throw new ArgumentNullException("dependencyInjector");
            }

            throw new NotImplementedException();
        }

        public void Dispose()
        {
            m_UnityContainer.Dispose();
            GC.SuppressFinalize(this);
        }
    }
}
